package com.ebuy.shopping.controller;

import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import com.ebuy.shopping.common.utils.Contants;
import com.ebuy.shopping.model.Category;
import com.ebuy.shopping.model.Product;
import com.ebuy.shopping.service.ICategoryService;
import com.ebuy.shopping.service.IProductService;

/**
 * 
 * @ClassName: ProductController
 * @Description: TODO
 * @author wenzhao
 * @date 2017年8月11日 下午1:14:31
 * 
 */
@Controller
@RequestMapping("/product")
public class ProductController
{
    @Autowired
    private IProductService productService;
    @Autowired
    private ICategoryService categoryService;

    @RequestMapping(value = "{id}/show", method = RequestMethod.GET)
    private String show(@PathVariable
    int id, Model model) throws Exception
    {
        Product Product = productService.load(id);
        model.addAttribute(Product);
        return "product/product_show";

    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model) throws Exception
    {
        List<Product> productList = productService.getProducts();
        model.addAttribute("productList", productList);
        return "product/product_list";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Model model) throws Exception
    {

        List<Category> gradeList = categoryService
                .getCategoriesByGrade(Contants.GRADE_ONE);
        model.addAttribute("gradeList", gradeList);
        Map<Category, List<Category>> catMap = new HashMap<Category, List<Category>>();
        for (Category cat : gradeList)
        {
            int catId = cat.getId();
            List<Category> catList = categoryService
                    .getCategoriesByParentId(catId);
            catMap.put(cat, catList);

        }
        model.addAttribute("catMap", catMap);
        return "product/product_add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(Product product, MultipartFile cover) throws Exception
    {
        System.out.println("文件名：" + cover.getOriginalFilename());
        System.out.println("文件类型：" + cover.getContentType());
        String fileName = cover.getOriginalFilename();
        FileOutputStream fos = new FileOutputStream(Contants.FILE_SAVE_URL
                + cover.getOriginalFilename());
        FileCopyUtils.copy(cover.getInputStream(), fos);
        product.setFileName(fileName);
        int flag = productService.add(product);
        if (flag > 0)
        {
            System.out.println("添加成功！");
        }
        else
        {
            System.out.println("添加失败！");
        }
        return "redirect:/product/list";
    }

    /*
     * 到修改页面
     */
    @RequestMapping(value = "{id}/update", method = RequestMethod.GET)
    public String update(@PathVariable
    int id, Model model) throws Exception
    {
        List<Category> gradeList = categoryService
                .getCategoriesByGrade(Contants.GRADE_ONE);
        model.addAttribute("gradeList", gradeList);
        Map<Category, List<Category>> catMap = new HashMap<Category, List<Category>>();
        for (Category cat : gradeList)
        {
            int catId = cat.getId();
            List<Category> catList = categoryService
                    .getCategoriesByParentId(catId);
            catMap.put(cat, catList);

        }
        model.addAttribute("catMap", catMap);
        Product product = productService.load(id);
        model.addAttribute("product", product);

        return "product/product_update";
    }

    /*
     * 执行修改
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Product product, MultipartFile cover) throws Exception
    {

        String fileName = cover.getOriginalFilename();
        System.out.println("fileName:" + fileName);
        if (!"".equals(fileName))
        {
            FileOutputStream fos = new FileOutputStream(Contants.FILE_SAVE_URL
                    + cover.getOriginalFilename());
            FileCopyUtils.copy(cover.getInputStream(), fos);
            product.setFileName(fileName);
        }

        productService.update(product);
        return "redirect:/product/list";
    }

    /*
     * 执行修改
     */
    @RequestMapping(value = "{id}/delete", method = RequestMethod.GET)
    public String delete(@PathVariable
    int id) throws Exception
    {

        int flag = productService.deleteByPrimaryKey(id);
        if (flag > 0)
        {

        }

        return "forward:/product/list";
    }

}
